<?php
/**
 * Created by PhpStorm.
 * User: zlkmu
 * Date: 2018/1/28
 * Time: 17:14
 */
use Workerman\Worker;
use \GatewayWorker\Lib\Gateway;
use \GatewayWorker\BusinessWorker;
require_once __DIR__ . '/../Protocols/ProtocolAPI.php';
require_once __DIR__ . '/../Protocols/GameType.php';
require_once __DIR__ . '/../Protocols/Status.php';
require_once __DIR__.'/../Channel/Client.php';
require_once __DIR__.'/../Channel/Server.php';
require_once __DIR__.'/../Module/User.php';
require_once __DIR__.'/../Player/PlayerAvatar.php';
require_once 'ServerContext.php';


class PublicServer{
    //存放玩家信息的管理类
    private $serverContext;
    private $userDBModule;

    function __construct()
    {
        $this->serverContext = new ServerContext();
        $this->userDBModule = new User();
    }

    /**
     * 公共消息处理转发器
     * @param $router_msg 来自MsgRouteServer转发的信息
     * @return array array(array(0=>返回头部，1=>状态，2=>游戏类型, 3=>连接ID/用户ID，4=>具体结果))
     */
    public function processPublicRequest($router_msg){
        $connectionID = $router_msg['clientID'];
        $clientMsg = $router_msg['clientMsg'];
        switch($clientMsg['head_request']){
            //登录请求
            case ProtocolAPI::$LOGIN_REQUEST:
               return $this->userLogin($connectionID, $clientMsg, $router_msg['remoteIP']);
            //检查版本请求
            case ProtocolAPI::$VERSION_CHECK_REQUEST:
                return $this->getGameVersion($connectionID);
            //获取大厅通告请求
            case ProtocolAPI::$GET_NOTICE_REQUEST:
                return $this->getNotice($connectionID);
            //获取钻石排行请求
            case ProtocolAPI::$DIAMOND_PAIHANG_REQUEST:
                return $this->getDiamondRanking($connectionID);
            //微信分享请求
            case ProtocolAPI::$SHARE_REQUEST:
                return $this->shareRequest($connectionID);
            //玩家实名认证请求
            case ProtocolAPI::$REAL_NAME_CERTIFY_REQUEST:
                return $this->realNameAuth($connectionID, $clientMsg);
            //钻石换房卡请求
            case ProtocolAPI::$DIAMOND_TO_CARDS_REQUEST:
                return $this->diamondToCards($connectionID, $clientMsg);
            //检测到用户是否掉线
            case ProtocolAPI::$CHECK_ONLINE_REQUEST:
                return $this->checkOnLineRequest($connectionID);
            //刷新用户房卡和钻石数量的请求
            case ProtocolAPI::$REFRESH_DIAMOND_CARD_REQUEST:
                return $this->refreshDiamondCardRequest($connectionID);
            //用户断线重连请求
            case ProtocolAPI::$BOLT_CONNECT_REQUEST:
                return $this->boltConnectRequest($connectionID, $clientMsg);
            //用户登出请求
            case ProtocolAPI::$USER_LOGOUT_REQUEST:
                return $this->userLogOut($connectionID);







        }
    }

    /**
     * 处理登录的函数
     * @param $connectionID 连接ID
     * @param $clientMsg 客户端登陆请求
     * @return array(array(0=>返回头部，1=>状态，2=>游戏类型, 3=>连接ID/用户ID，4=>具体结果))
     */
    private function userLogin($connectionID, $clientMsg, $remoteIP){
        //检查玩家是否注册过
        $userID = $this->userDBModule->judgeUser($clientMsg["client_msg"]);
        echo "------------".$userID."\n";
        if($userID){
            //如果注册过，检查玩家是否在serverContext中
            if($this->serverContext->getPlayer($userID) != null){
                if($this->serverContext->isPlayerOnLine($userID)){
                    $status = false;
                }
                else{
                    //玩家从上线
                    $this->serverContext->playerOnLine($userID);
                    //返回玩家信息
                    $status = true;
                }
            }
            //如果不在serverContext中，玩家为全新登陆
            else{
                $userID = $this->serverContext->addPlayerAvatar($clientMsg, $remoteIP);
                $status = $userID;
            }
        }
        //玩家没有注册过，添加玩家信息（顺便注册玩家信息)
        else{
            $userID = $this->serverContext->addPlayerAvatar($clientMsg, $remoteIP);
            $status = $userID;
        }
        if(!$status){
            return array(array(ProtocolAPI::$ERROR_RESPONSE, Status::$STATUS_FAIL,
                GameType::$ALLGame, $connectionID, "ProtocolAPI::LOGIN_REQUEST::NO_SUCH_USER" ));
        }
        else{
            \GatewayWorker\Lib\Gateway::bindUid($connectionID, $userID);
            return array(array(ProtocolAPI::$LOGIN_RESPONSE,Status::$STATUS_OK,
                GameType::$ALLGame, $userID, $this->serverContext->getPlayer($userID)));
        }

    }

    /**
     * 检查游戏版本号
     * @param $connectionID 连接ID
     * @return array(array(0=>返回头部，1=>状态，2=>游戏类型, 3=>连接ID/用户ID，4=>具体结果))
     */
    private  function getGameVersion($connectionID){
        $system = new System();
        return array(array(ProtocolAPI::$VERSION_CHECK_RESPONSE, Status::$STATUS_OK,
            GameType::$ALLGame, $connectionID, $system->getVersion()));
    }

    /**
     * 获取大厅公告通知数据
     * @param $connectionID
     * @return array(array(0=>返回头部，1=>状态，2=>游戏类型, 3=>连接ID/用户ID，4=>具体结果))
     */
    private function getNotice($connectionID){
        $userUid=$_SESSION["$connectionID"];//得到玩家uid
        if($userUid){
            $systemModel=new System();
            $notice=$systemModel->getNotice();//得到通知消息
            $r["notice"]=$notice;
            return array(array(ProtocolAPI::$GET_NOTICE_RESPONSE, Status::$STATUS_OK, GameType::$ALLGame, $userUid, $r));
        }
        return array(array(ProtocolAPI::$ERROR_RESPONSE, Status::$STATUS_FAIL, GameType::$ALLGame, $connectionID,
            "ProtocolAPI::GET_NOTICE_REQUEST::NO_SUCH_USER"));
    }

    /**
     * 获取钻石排行榜请求
     * @param $connectionID
     * @return array(array(0=>返回头部，1=>状态，2=>游戏类型, 3=>连接ID/用户ID，4=>具体结果))
     */
    private function getDiamondRanking($connectionID){
        //得到玩家uid
        $userUid=$_SESSION["$connectionID"];
        if($userUid){
            //在数据库中查询数据
            $userModel=new User();
            $r=$userModel->queryPaiHang();
            return array(array(ProtocolAPI::$DIAMOND_PAIHANG_RESPONSE, Status::$STATUS_OK, GameType::$ALLGame, $userUid, $r));
        }

        return array(array(ProtocolAPI::$ERROR_RESPONSE, Status::$STATUS_FAIL, GameType::$ALLGame, $connectionID,
            "ProtocolAPI::DIAMOND_PAIHANG_REQUEST::NO_SUCH_USER"));
    }

    /**
     * 用户分享请求
     * @param $connectionID
     * @param $clientRequest
     * @param $gameServer
     * @return array(array(0=>返回头部，1=>状态，2=>游戏类型, 3=>连接ID/用户ID，4=>具体结果))
     */
    private function shareRequest($connectionID){
        $userUid=$_SESSION["$connectionID"];//得到玩家uid
        if($userUid){
            //更新并获取玩家共享信息
            $shareResult = $this->serverContext->updatePlayerShareTime($userUid);
            return array(array(ProtocolAPI::$SHARE_RESPONSE, Status::$STATUS_OK, GameType::$ALLGame, $userUid, $shareResult[0]));

        }
        return array(array(ProtocolAPI::$ERROR_RESPONSE, Status::$STATUS_FAIL, GameType::$ALLGame, $connectionID,
            "ProtocolAPI::SHARE_REQUEST::NO_SUCH_USER"));
    }

    /**
     * 身份证实名认证
     * @param $connectionID
     * @param $clientRequest
     * @param $gameServer
     * @return array(array(0=>返回头部，1=>状态，2=>游戏类型, 3=>连接ID/用户ID，4=>具体结果))
     */
    private function realNameAuth($connectionID,$clientRequest){
        $userUid = $_SESSION["$connectionID"];//得到玩家uid
        $userName = $clientRequest["client_msg"]["userName"];//接收到的用户真实名
        $idCard = $clientRequest["client_msg"]["idCard"];//接收到的用户身份证号
        if($userUid && !empty($userName) && !empty($idCard)) {
            $r = $this->serverContext->realNameAuth($userUid, $userName, $idCard);
            if($r){
                return array(array(ProtocolAPI::$REAL_NAME_CERTIFY_RESPONSE, Status::$STATUS_OK, GameType::$ALLGame, $userUid, null));
            }
            else{
                return array(array(ProtocolAPI::$REAL_NAME_CERTIFY_RESPONSE, Status::$STATUS_FAIL, GameType::$ALLGame, $userUid, null));
            }
        }
        return array(array(ProtocolAPI::$ERROR_RESPONSE, Status::$STATUS_FAIL, GameType::$ALLGame, $connectionID,
            "ProtocolAPI::REAL_NAME_CERTIFY_REQUEST::NO_SUCH_USER"));
    }


    /**
     * 响应用户是否还在线
     * @param $connectionID
     * @return array(array(0=>返回头部，1=>状态，2=>游戏类型, 3=>连接ID/用户ID，4=>具体结果))
     */
    private function checkOnLineRequest($connectionID){
        return array(array(ProtocolAPI::$CHECK_ONLINE_RESPONSE,Status::$STATUS_OK,GameType::$ALLGame,$connectionID,null));
    }

    /**
     * 钻石换房卡请求函数
     * @param $connectionID
     * @param $clientRequest
     * @param $gameServer
     * @return array(array(0=>返回头部，1=>状态，2=>游戏类型, 3=>连接ID/用户ID，4=>具体结果))
     */
    private function diamondToCards($connectionID, $clientRequest){
        //查找到当前创建房间人的UID
        $userUid=$_SESSION["$connectionID"];

        if($userUid) {
            $r = $this->serverContext->diamondToCard($userUid, $clientRequest);
            if($r){
                return array(array(ProtocolAPI::$DIAMOND_TO_CARDS_RESPONSE, Status::$STATUS_OK,
                    GameType::$ALLGame, $userUid, $r));
            }
            else{
                return array(array(ProtocolAPI::$DIAMOND_TO_CARDS_RESPONSE, Status::$STATUS_FAIL,
                    GameType::$ALLGame, $userUid, null));
            }

        }
        return array(array(ProtocolAPI::$ERROR_RESPONSE, Status::$STATUS_FAIL, GameType::$ALLGame, $connectionID,
            "ProtocolAPI::DIAMOND_TO_CARDS_REQUEST::NO_SUCH_USER"));
    }

    /**
     * 刷新玩家的砖石和房卡请求
     * @param $connectionID
     * @return array(array(0=>返回头部，1=>状态，2=>游戏类型, 3=>连接ID/用户ID，4=>具体结果))
     */
    private function refreshDiamondCardRequest($connectionID){
        $userUid = $_SESSION["$connectionID"];//得到玩家uid
        if($userUid){
            $r = $this->serverContext->refreshDiamondAndCard($userUid);
            if($r){
                return array(array(ProtocolAPI::$REFRESH_DIAMOND_CARD_RESPONSE,Status::$STATUS_OK,GameType::$ALLGame,
                    $userUid,$r));
            }
            else{
                return array(array(ProtocolAPI::$REFRESH_DIAMOND_CARD_RESPONSE,Status::$STATUS_FAIL,GameType::$ALLGame,
                    $userUid,null));
            }
        }
        return array(array(ProtocolAPI::$ERROR_RESPONSE, Status::$STATUS_FAIL, GameType::$ALLGame, $connectionID,
            "ProtocolAPI::REFRESH_DIAMOND_CARD_REQUEST::NO_SUCH_USER"));
    }

    /**
     * 断线重连请求
     * @param $connectionID
     * @param $clientRequest
     * @return array(array(0=>返回头部，1=>状态，2=>游戏类型, 3=>连接ID/用户ID，4=>具体结果))
     */
    private function boltConnectRequest($connectionID,$clientRequest){

        $userUid = $clientRequest["client_msg"];//掉线玩家的UID
        $r = $this->serverContext->playerOnLine($userUid);
        if($r){
            $_SESSION[$connectionID] = $userUid;
            \GatewayWorker\Lib\Gateway::bindUid($connectionID,$userUid);
            return array(array(ProtocolAPI::$BOLT_CONNECT_RESPONSE, Status::$STATUS_OK,
                GameType::$ALLGame, $userUid, null));
        }
        else{
            return array(array(ProtocolAPI::$ERROR_RESPONSE, Status::$STATUS_FAIL, GameType::$ALLGame, $connectionID,
                "ProtocolAPI::BOLT_CONNECT_REQUEST::NO_SUCH_USER"));
        }

    }

    /**
     * 玩家退出登录请求
     * @param $connectionID
     * @return array(array(0=>返回头部，1=>状态，2=>游戏类型, 3=>连接ID/用户ID，4=>具体结果))
     */
    private function userLogOut($connectionID){
        $userUID = $_SESSION["$connectionID"];
        if($userUID){
            $r = $this->serverContext->playerLogOut($userUID);
            if($r){
                return array(array(ProtocolAPI::$USER_LOGOUT_RESPONSE, Status::$STATUS_OK,
                    GameType::$ALLGame, $connectionID, null));
            }
            else{
                return array(array(ProtocolAPI::$USER_LOGOUT_RESPONSE, Status::$STATUS_FAIL,
                    GameType::$ALLGame, $connectionID, null));
            }
        }
        else{
            return array(array(ProtocolAPI::$ERROR_RESPONSE, Status::$STATUS_FAIL, GameType::$ALLGame, $connectionID,
                "ProtocolAPI::USER_LOGOUT_REQUEST::NO_SUCH_USER"));
        }
    }
}



?>